package my.jypark.seminar;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: jypark
 * Date: 13. 10. 29
 * Time: 오후 2:02
 * To change this template use File | Settings | File Templates.
 */
public class LineNumberTableAttribute extends Attribute {
    private int lineNumLen;
    private short [] lnumTable;
    public LineNumberTableAttribute(String name, DataInputStream ins) throws IOException {
        super(name, ins);
    }

    @Override
    protected void readData(DataInputStream ins, int len) throws IOException {
        lineNumLen = ins.readShort();
        lnumTable = new short [lineNumLen*2];

        for(int i=0; i<lineNumLen; i++) {
            lnumTable[i*2] = ins.readShort();   // start PC
            lnumTable[i*2+1] = ins.readShort(); // line number
        }
    }

    @Override
    public int getLen() {
        return lineNumLen;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("\n---- Line Number Table ----\n");
        sb.append("table length : ").append(lineNumLen).append('\n');

        for(int i=0; i<lineNumLen; i++) {
            sb.append("\nstart PC : ").append(lnumTable[i*2]).append('\n');   // start PC
            sb.append("line number : ").append(lnumTable[i*2+1]).append('\n'); // line number
        }

        return sb.toString();
    }
}
